package darknet.core.impl.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import darknet.core.spec.net.Message;
import darknet.core.spec.net.StreamableMessage;

/**
 * Implementation of a streamable message.
 * 
 * @author Yoyo
 */
@SuppressWarnings("serial")
public class StreamableMessageImpl implements StreamableMessage {

    /** The message */
    private Message hook;
    
    /** Input stream. */
    private InputStream input;

    /** Output stream. */
    private OutputStream output;
    
    /**
     * Initialise a abstract streamable message.
     * 
     * @param toWrap	The message.
     * @param input	The input stream.
     * @param output	The output stream.
     */
    public StreamableMessageImpl(Message toWrap, InputStream input,
	    OutputStream output) {
	
	if (toWrap == null) {
	    throw new IllegalArgumentException("message is null");
	}
	this.hook = toWrap;
	
	if (input == null) {
	    throw new IllegalArgumentException("input is null");
	}
	this.input = input;
	
	if (output == null) {
	    throw new IllegalArgumentException("output is null");
	}
	this.output = output;
    }

    @Override
    public Message getWrapped() {
	return hook;
    }

    @Override
    public InputStream getInput() {
	return input;
    }

    @Override
    public OutputStream getOutput() {
	return output;
    }

    @Override
    public void close() throws IOException {
	input.close();
	output.close();
    }

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder("StreamableMessageImpl[");
        builder.append("hook=");
        builder.append(hook.toString());
        builder.append("]");

        return builder.toString();
    }
}
